#include "../includes/new_queue.h"

void init_queue(queue_t *q){
    q->len = 0;
}


void insert_queue(queue_t *q, char* user_name, char* mess, FILE *Q_FILE){
    if (q->len < MAX_SIZE-1){
        strcpy(q->queue[q->len].mess, mess);
        strcpy(q->queue[q->len].user_name, user_name);
    }
    else{
        perror("QUEUE IS FULL");
        exit(EXIT_FAILURE);
    }
    
    Q_FILE = open(PATH_TO_QUEUE, "w+");
    for (int i = 0; i < q->len; ++i){
        fprintf(Q_FILE, "%d\n", q[i]->DBid);   
    }
}


int is_empty_queue(const queue_t *q){
    if (q->len <= 0)
        return 1;
    else
        return 0;
}


void remove_queue(queue_t *q, int fd){
    for (int i = 0; i < q->len; ++i){
        strcpy(q->queue[i].str, q->queue[i+1].str);
        q->queue[i].fd = q->queue[i+1].fd;
    }

    q->fd[fd] = 0;
    q->len--;
}


int is_in_queue(queue_t *q, int fd){
    if (q->fd[fd] == 1)
        return 1;
        
    return 0;
}
